package me.twoday.library.mine.adapter;

import java.util.LinkedHashMap;
import java.util.Map;

import me.twoday.library.mine.handler.SettingHandler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

/**
 * SettingActivity만을 위한 SettingListAdapter 
 * 
 * @author 김학성
 * @since 2012. 4. 18.
 */
@SuppressWarnings("unused")
public class SettingListAdapter extends BaseAdapter implements OnClickListener
{
	private static final String TAG = "SettingListAdapter";
	private SettingHandler mListHandler;
	public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
	public final ArrayAdapter<String> headers;
	public final static int TYPE_SECTION_HEADER = 0;
	public static class Tagging
	{
		public boolean isSub;
		public String superTitle;
		public int subTitleIndex;
	}
	
	@SuppressWarnings("static-access")
	public SettingListAdapter(SettingHandler listHandler)
	{
		this.mListHandler = listHandler;
		headers = new ArrayAdapter<String>(listHandler.getActivity(), mListHandler.SETTING_LIST_SUPER_ROW);
		
	}

	public void addSection(String section, Adapter adapter) 
	{
		this.headers.add(section);
		this.sections.put(section, adapter);
	}

	// 구현하지 않아도 잘 돌아가는 getItem함수....................;;
	public Object getItem(int position) 
	{
		for(Object section : this.sections.keySet()) 
		{
			Adapter adapter = sections.get(section);
			// header 까지 포함해야 하기 때문에 1을 더해주는건가?
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0) return section;
			if(position < size) return adapter.getItem(position - 1);

			// otherwise jump into next section
			position -= size;
		}
		return null;
	}

	public int getCount() 
	{
		// total together all sections, plus one for each section header
		int total = 0;
		for(Adapter adapter : this.sections.values())
			total += adapter.getCount() + 1;
		return total;
	}

	public int getViewTypeCount() 
	{
		// assume that headers count as one, then total all sections
		int total = 1;
		for(Adapter adapter : this.sections.values())
			total += adapter.getViewTypeCount();
		return total;
	}

	public int getItemViewType(int position) 
	{
		int type = 1;
		for(Object section : this.sections.keySet()) 
		{
			Adapter adapter = sections.get(section);
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0) return TYPE_SECTION_HEADER;
			if(position < size) return type + adapter.getItemViewType(position - 1);

			// otherwise jump into next section
			position -= size;
			type += adapter.getViewTypeCount();
		}
		return -1;
	}

	public boolean areAllItemsSelectable() 
	{
		return false;
	}

	public boolean isEnabled(int position) 
	{
		return (getItemViewType(position) != TYPE_SECTION_HEADER);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		int sectionnum = 0;
		Tagging tagging = new Tagging();

		for(Object section : this.sections.keySet()) 
		{
			Adapter adapter = sections.get(section);
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0)
			{
				convertView = headers.getView(sectionnum, convertView, parent);
				tagging.isSub = false;
				convertView.setTag(tagging);
				convertView.setOnClickListener(this);
				return convertView;
			}
			if(position < size)
			{
				convertView = adapter.getView(position - 1, convertView, parent);
				tagging.isSub = true;
				tagging.superTitle = section.toString();
				tagging.subTitleIndex = position;
				convertView.setTag(tagging);
				convertView.setOnClickListener(this);
				return convertView;
			}
			// otherwise jump into next section
			position -= size;
			sectionnum++;
		}
		return null;
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}

	@Override
	public void onClick(View v) 
	{
		Tagging tagging = (Tagging)v.getTag();
		if(!tagging.isSub)
		{
			return;
		}
		else
		{
			mListHandler.onRowClick(v);
		}
	}
}
